package demo02.jdbc;

import demo02.entity.User;
import demo02.tools.Const;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

//只负责数据增删改查
public class UseJdbc {

    static {
        try {
            Class.forName(Const.DRIVER);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * 通过登录名查找用户
     *
     * @param loginName
     * @return
     */
    public User selectUser(String loginName) {
        return selectUser(loginName, null);
    }

    /**
     * 通过登录名查找用户
     *
     * @param loginName
     * @return
     */
    public User selectUser(String loginName, String password) {

        //初始化一个User类型对象
        User user = null;

        try (
                Connection conn = DriverManager.getConnection(
                        Const.URL_TEST,
                        Const.USERNAME,
                        Const.PASSWORD
                );
                Statement statement = conn.createStatement();


        ) {
            String sql = "select * from t_user where loginName='" + loginName + "' ";
            //如果密码不为空，加密码的条件，这里的 "" 根据实际情况决定是否添加
            if (password != null) {
                sql += " and password='" + password + "'";
            }
            ResultSet rs = statement.executeQuery(sql);
            //如果返回的rs不为空，封装User
            if (rs.next()) {
                user = new User(
                        rs.getInt(1),
                        rs.getString(2),
                        rs.getString(3),
                        rs.getString(4)
                );
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return user;
    }

    /**
     * 向表中添加数据
     *
     * @param user
     * @return
     */
    public int addUser(User user) {

        int result = 0;

        try (
                Connection conn = DriverManager.getConnection(
                        Const.URL_TEST,
                        Const.USERNAME,
                        Const.PASSWORD
                );
                Statement statement = conn.createStatement();
        ) {
            String sql = "insert into t_user values(" +
                    "'" + user.getId() + "'," +
                    "'" + user.getUserName() + "'," +
                    "'" + user.getPassword() + "'," +
                    "'" + user.getEmail() + "'," +
                    ")";
            System.out.println(sql + "=========");
            result = statement.executeUpdate(sql);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    /**
     * 登录时更新登录次数和时间
     *
     * @return
     */
    public int updateLoginCountAndTime(String userno) {
        int result = 0;

        try (
                Connection conn = DriverManager.getConnection(
                        Const.URL_TEST,
                        Const.USERNAME,
                        Const.PASSWORD
                );
                Statement statement = conn.createStatement();
        ) {
            String nowTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            String sql = "update t_user set loginCount=loginCount+1,lastLoginTime='" + nowTime + "' where userno='" + userno + "'";
            result = statement.executeUpdate(sql);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}

